﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using  Kunlun.Models;
using System.Collections.Generic;
namespace Kunlun.BusinessLogic
{

    public static class KunlunHtml
    {
        public static MvcHtmlString Image(string url, string domain, string alternateText)
        {
            return Image("image", url,domain, alternateText, null);
        }
        public static MvcHtmlString Image(string id, string imagepath, string domain, string alternateText, object htmlAttributes)
        {
            var builder = new TagBuilder("img");
            builder.GenerateId(id);           
            builder.MergeAttribute("src", domain + imagepath);
            builder.MergeAttribute("alt", alternateText);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        
        }
        public static MvcHtmlString Pager(this HtmlHelper htmlHelper,PageModel model,string control,string action)
        {
            var builder = new TagBuilder("div");
            builder.AddCssClass("PageDiv");
            int pagecount=Convert.ToInt32(Math.Ceiling(model.ItemCount/(decimal)model.PageSize));
            int beginpage = 1;
            int len = 10;
            int endpage = model.PageIndex;
            string firstpagetext = string.Empty;
            string endpagetext = string.Empty;
            if (len /2 < model.PageIndex)
            {
                beginpage = model.PageIndex - len / 2;
                firstpagetext = "<a  class=\"FirstPage\" href=\"/" + control + "/" + action + "/" + model.ID + "/1\">第一页</a>...";
            }
            if (model.PageIndex + len / 2 < pagecount)
            {
                endpage = model.PageIndex + len / 2;
                endpagetext = "...<a  class=\"EndPage\" href=\"/" + control + "/" + action + "/" + model.ID + "/" + pagecount + "\">最后一页</a>";
            }
            else
            {
                endpage = pagecount;
            }
            builder.InnerHtml = firstpagetext;
            for (int i = beginpage; i < endpage+1; i++)
            {
                if (i == model.PageIndex+1)
                {
                    builder.InnerHtml = builder.InnerHtml + "<a class=\"pageNumber\" href=\"/" + control + "/" + action + "/" + model.ID + "/" + i + "\"><span class=\"thispage\">" + i + "</span></a>"; 
                }
                else
                {
                    
                    builder.InnerHtml = builder.InnerHtml + "<a class=\"pageNumber\" href=\"/" + control + "/" + action + "/" + model.ID + "/" + i + "\">" + i + "</a>";
                }
            }
            builder.InnerHtml = builder.InnerHtml + endpagetext;
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
            
        }
        
    }
}
